import { ref, onMounted, onDeactivated, onActivated, onUnmounted } from 'vue'
import cache from '@/utils/cache'
import { useRoute } from 'vue-router'

// 传入当前位置
export default function () {
  const scrollTop = ref<number>(0)
  const route = useRoute()
  const name = route.name as string

  // 记录当前的位置
  onMounted(() => {
    // 重返当前位置
    const currScroll = cache.getItem(name) ?? 0
    window.scrollTo(0, currScroll)
    window.addEventListener(
      'scroll',
      () => {
        scrollTop.value = document.documentElement.scrollTop
        cache.setItem(name, scrollTop.value)
      },
      false
    )
  })
}
